local jiaomen = fk.CreateSkill {
  name = "lingling__jiaomen",
}

Fk:loadTranslationTable{
  ["lingling__jiaomen"] = "叫门",
  [":lingling__jiaomen"] = "当其他角色对你造成伤害后，你可以令其手牌上限-2直到其回合结束，然后你令另一名其他角色交给其两张牌。",

  ["#lingling__jiaomen-invoke"] = "叫门：你可以令 %dest 手牌上限-2直到其回合结束",
  ["#lingling__jiaomen-choose"] = "叫门：你可以令一名角色交给 %dest 两张牌",
  ["#lingling__jiaomen-give"] = "叫门：请交给 %dest 两张牌",
}

jiaomen:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(jiaomen.name) and
      data.from and not data.from.dead and data.from ~= player
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local choice = room:askToChoice(player, {
      choices = { "-2", "Cancel"},
      skill_name = jiaomen.name,
      prompt = "#lingling__jiaomen-invoke::"..data.from.id,
    })
    if choice ~= "Cancel" then
      event:setCostData(self, {tos = {data.from}, choice = choice})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = event:getCostData(self).choice
    if choice == "+2" then
      room:addPlayerMark(data.from, jiaomen.name, 2)
    else
      room:setPlayerMark(data.from, jiaomen.name, data.from:getMark(jiaomen.name) - 2)
    end
    local targets = table.filter(room:getOtherPlayers(player, false), function (p)
      return p ~= data.from and #p:getCardIds("he") > 1
    end)
    if #targets == 0 then return end
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = targets,
      skill_name = jiaomen.name,
      prompt = "#lingling__jiaomen-choose::"..data.from.id,
      cancelable = false,
    })[1]
    room:doIndicate(to, {data.from})
    local cards = room:askToCards(to, {
      min_num = 2,
      max_num = 2,
      include_equip = true,
      skill_name = jiaomen.name,
      prompt = "#lingling__jiaomen-give::"..data.from.id,
      cancelable = false,
    })
    room:moveCardTo(cards, Card.PlayerHand, data.from, fk.ReasonGive, jiaomen.name, nil, false, to)
  end,
})

jiaomen:addEffect(fk.TurnEnd, {
  can_refresh = function (self, event, target, player, data)
    return target == player
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, jiaomen.name, 0)
  end,
})

jiaomen:addEffect("maxcards", {
  correct_func = function (self, player)
    return player:getMark(jiaomen.name)
  end,
})

return jiaomen
